package com.dc.controller;

import com.alibaba.fastjson.JSON;
import com.dc.bean.model.BaseModel;
import com.dc.bean.utils.BaseDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "DeleteAllServlet",urlPatterns = {"/deleteAllUser"})
public class DeleteAllServlet extends HttpServlet {
    BaseDao baseDao = new BaseDao();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] id = req.getParameterValues("id");
        for (int i = 0; i < id.length; i++) {
            String sql = "delete from t_user where id=" + id[i];
            int result = baseDao.update(sql);
            BaseModel baseModel = new BaseModel();
            if (result > 0) {
                //删除成功
                baseModel.setCode(1);
                baseModel.setMassage("删除用户成功");
            } else {
                //删除失败
                baseModel.setCode(0);
                baseModel.setMassage("删除用户失败");
            }
            String baseModelStr = JSON.toJSONString(baseModel);
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(baseModelStr);
        }
        resp.getWriter().flush();
        resp.getWriter().close();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
